package com.synjones.wschool.face.serviceImpl;

import com.synjones.wschool.face.constant.UserSourceEnum;
import com.synjones.wschool.face.domain.Student;
import com.synjones.wschool.face.domain.Teacher;
import com.synjones.wschool.face.service.EnoService;
import com.synjones.wschool.face.service.StudentService;
import com.synjones.wschool.face.service.TeacherService;
import com.synjones.wschool.face.service.dto.UserDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 24398 on 2018/8/3.
 */
@Service
@Slf4j
public class EnoServiceImpl implements EnoService {

    @Resource
    private TeacherService teacherService;
    @Resource
    private StudentService studentService;


    @Override
    public List<UserDTO> listEno(List<UserDTO> userDTOS) {
        if(userDTOS == null || userDTOS.isEmpty()){
            return Collections.emptyList();
        }
        List<UserDTO> userDTOSRet = new ArrayList<>();

        // 查询教师
        List<Long> teacherIds = userDTOS.parallelStream().filter(userDTO -> userDTO.getUserSourceId()
                                         .equals(UserSourceEnum.TEACHER.getUserSourceId()))
                                                 .map(UserDTO::getUserId)
                                                 .collect(Collectors.toList());

        List<UserDTO> teacherUserDTOs = this.listTeacherEnos(teacherIds);
        // 学生
        List<Long> studentIds = userDTOS.parallelStream().filter(userDTO -> userDTO.getUserSourceId()
                                         .equals(UserSourceEnum.STUDENT.getUserSourceId()))
                                                 .map(UserDTO::getUserId)
                                                 .collect(Collectors.toList());

       List<UserDTO > studentDTOs = this.listStudentEnos(studentIds);


       userDTOSRet.addAll(teacherUserDTOs);
       userDTOSRet.addAll(studentDTOs);

        return userDTOSRet;
    }

    private List<UserDTO> listTeacherEnos(List<Long> teacherIds){
        if(teacherIds == null || teacherIds.isEmpty()){
            return Collections.emptyList();
        }
        List<Teacher> teachers = this.teacherService.listTeacherByIds(teacherIds);
        List<UserDTO> teacherUserDTOS = teachers.parallelStream().map(teacher -> UserDTO.builder().userId(teacher.getSid())
                                                                   .userSourceId(UserSourceEnum.TEACHER.getUserSourceId())
                                                                   .eno(teacher.getEno())
                                                                   .userName(teacher.getCname())
                                                                   .build())
                                                                   .collect(Collectors.toList());
        return teacherUserDTOS;
    }

    private List<UserDTO> listStudentEnos(List<Long> studentIds){
        if(studentIds == null || studentIds.isEmpty()){
            return Collections.emptyList();
        }
        List<Student> students = this.studentService.listByIds(studentIds);
        List<UserDTO> studentUserDTOs = students.parallelStream().map(student -> UserDTO.builder().userId(student.getSid())
                                                                  .userSourceId(UserSourceEnum.STUDENT.getUserSourceId())
                                                                  .eno(student.getSno())
                                                                  .userName(student.getCname())
                                                                  .build())
                                                                  .collect(Collectors.toList());
        return studentUserDTOs;
    }

}
